package de.dushuhu.dyndns;

import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Named;
import com.google.inject.name.Names;

public class AgentModule extends AbstractModule {

	@Override
	protected void configure() {

		bind(UpdateService.class).to(UpdateServiceImpl.class);
		bind(CommandLineParser.class).to(PosixParser.class);
		// bind(Options.class).toInstance(buildOptions());

		// cfgTest();
		cfgReal();

		return;
	}

	@Provides
	@Named("help")
	String buildHelp() {
		return "java -jar dyndns-agent.jar -h hostname -u username -p password [ip regex]";
	}

	@Provides
	Options buildOptions(@Named("hostname") Option hostnameOption,
			@Named("username") Option usernameOption,
			@Named("password") Option passwordOption) {

		Options options = new Options();
		options.addOption(hostnameOption);
		// options.addOption(ipOption);
		options.addOption(usernameOption);
		options.addOption(passwordOption);
		// options.addOption(helpOption);

		return options;
	}

	@Provides
	@Named("hostname")
	Option buildHostnameOption() {

		Option option = new Option("h", "hostname", true,
				"hostname, e.g. renfeng.ath.cx");
		option.setRequired(true);

		return option;
	}

	// @Provides
	// @Named("ip")
	// Option buildIpOption() {
	//
	// Option option = new Option(null, "ip", true,
	// "ip Address, e.g. 1.2.3.4, default is to auto detect");
	// option.setRequired(true);
	//
	// return option;
	// }

	@Provides
	@Named("username")
	Option buildUsernameOption() {

		Option option = new Option("u", "username", true, "username of dyndns");
		option.setRequired(true);

		return option;
	}

	@Provides
	@Named("password")
	Option buildPasswordOption() {

		Option option = new Option("p", "password", true, "password of dyndns");
		option.setRequired(true);

		return option;
	}

	// @Provides
	// @Named("help")
	// Option buildHelpOption() {
	//
	// Option option = new Option("h", "help", true,
	// "display this help and exit");
	// option.setRequired(true);
	//
	// return option;
	// }

	private void cfgTest() {

		/*- see https://www.dyndns.com/developers/testaccount.html
		 * test.dyndns.org
		 * test.ath.cx
		 * test.dnsalias.net
		 * test.dnsalias.org
		 * test.dnsalias.com
		 * test.homeip.net
		 * test.mine.nu
		 * test.merseine.nu
		 * test.shacknet.nu
		 */
		bind(String.class).annotatedWith(
				Names.named(UpdateService.DYNDNS_SERVER_ADDRESS))//
				.toInstance("test.dyndns.org");
		bind(Integer.class).annotatedWith(
				Names.named(UpdateService.DYNDNS_SERVER_PORT))//
				.toInstance(80);
		bind(String.class).annotatedWith(
				Names.named(UpdateService.DYNDNS_SERVER_USERNAME))//
				.toInstance("test");
		bind(String.class).annotatedWith(
				Names.named(UpdateService.DYNDNS_SERVER_PASSWORD))//
				.toInstance("test");

		return;
	}

	private void cfgReal() {

		bind(String.class).annotatedWith(
				Names.named(UpdateService.DYNDNS_SERVER_ADDRESS))//
				.toInstance("members.dyndns.org");
		bind(Integer.class).annotatedWith(
				Names.named(UpdateService.DYNDNS_SERVER_PORT))//
				.toInstance(80);

		return;
	}

}
